package com.yanqu.road.dao.impl.activity.zhenhuan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.UserZhenHuanPartyHistoryData;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.UserZhenHuanSecondProcessData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserZhenHuanPartyHistoryDaoImpl extends TempDataObjectDao<UserZhenHuanPartyHistoryData> {

    public List<UserZhenHuanPartyHistoryData> getNeedAutoMailDataList(int activityId) {
        String sql = "select * from `t_u_zhenhuan_party_history` where `activityId`=? and `getTime`=0";
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserZhenHuanPartyHistoryData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                dataList.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String getUserZhenHuanPartyHistoryDataSql = "select * from `t_u_zhenhuan_party_history` where `activityId`=? and `userId`=?";

    public Map<Long, UserZhenHuanPartyHistoryData> getUserZhenHuanPartyHistoryData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserZhenHuanPartyHistoryData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserZhenHuanPartyHistoryDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserZhenHuanPartyHistoryData data = parse(rs);
                dataMap.put(data.getPartyId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserZhenHuanPartyHistoryDataSql = "update `t_u_zhenhuan_party_history` set pos=?,getTime=?,reward=?,finishTime=? where activityId=? and userId=? and partyId=?";
    @Override
    protected boolean updateData(UserZhenHuanPartyHistoryData userZhenHuanPartyHistoryData) {
        userZhenHuanPartyHistoryData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanPartyHistoryData.getPos()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanPartyHistoryData.getGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, userZhenHuanPartyHistoryData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanPartyHistoryData.getFinishTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanPartyHistoryData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanPartyHistoryData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanPartyHistoryData.getPartyId()));
        if (getDb().executeNoneQuery(updateUserZhenHuanPartyHistoryDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserZhenHuanPartyHistoryDataSql = "insert into `t_u_zhenhuan_party_history`(activityId,userId,partyId,pos,getTime,reward,finishTime) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserZhenHuanPartyHistoryData userZhenHuanPartyHistoryData) {
        userZhenHuanPartyHistoryData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanPartyHistoryData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanPartyHistoryData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanPartyHistoryData.getPartyId()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanPartyHistoryData.getPos()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanPartyHistoryData.getGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, userZhenHuanPartyHistoryData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanPartyHistoryData.getFinishTime()));
        if (getDb().executeNoneQuery(insertUserZhenHuanPartyHistoryDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserZhenHuanPartyHistoryData parseData(ResultSet rs) throws SQLException {
        UserZhenHuanPartyHistoryData userZhenHuanPartyHistoryData = new UserZhenHuanPartyHistoryData();
        userZhenHuanPartyHistoryData.setActivityId(rs.getInt("activityId"));
        userZhenHuanPartyHistoryData.setUserId(rs.getLong("userId"));
        userZhenHuanPartyHistoryData.setPartyId(rs.getLong("partyId"));
        userZhenHuanPartyHistoryData.setPos(rs.getInt("pos"));
        userZhenHuanPartyHistoryData.setGetTime(rs.getLong("getTime"));
        userZhenHuanPartyHistoryData.setReward(rs.getString("reward"));
        userZhenHuanPartyHistoryData.setFinishTime(rs.getLong("finishTime"));

        return userZhenHuanPartyHistoryData;
    }

    public boolean batchAdd(List<UserZhenHuanPartyHistoryData> dataList) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (UserZhenHuanPartyHistoryData data : dataList) {
            int i = 1;
            Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getPartyId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getPos()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getGetTime()));
            para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(insertUserZhenHuanPartyHistoryDataSql, mapList) > -1) {
            return true;
        }
        return false;
    }
}
